package com.sheng.center.action;


import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sheng.center.entity.User;
import com.sheng.center.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	
	protected String forwardView;
	protected static final String MAIN_JSP = "/WEB-INF/page/main.jsp";
	protected static final String STUDENT_MAIN_JSP = "/WEB-INF/page/Student_Main.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/User_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/User_edit.jsp";
	protected static final String LIST_JSP = "/WEB-INF/page/User_list.jsp";
    private User user = new User();
	private UserService userService;
	private List<User> list;
	
	public User getModel() {
		return user;
	}
	
	public String openAdd(){
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	public String openEdit(){
		user = userService.findById(user);
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	/**
	 * 
	 * @Title: register
	 * @Description: 注册、添加用户
	 * @param @return 参数
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月9日 下午4:55:15 
	 * @throws
	 */
	public String register(){
		boolean register = userService.register(user);
		if(register){
			return list();
		}else{
			return ERROR;
		}
	}
	/**
	 * 
	 * @Title: login
	 * @Description: 登录
	 * @param @return 参数
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月9日 下午4:55:49 
	 * @throws
	 */
	public String login(){
		User u = null;
		u = userService.login(user);
		System.out.println("账号密码==="+user.getUsername()+";"+user.getPassword());
		ActionContext.getContext().getSession().put("LoginUser", u.getUsername());
		if(u.getIsUser() == 0) {
			//管理员
			System.out.println("登录人员=="+u.getUsername()+";"+u.getPassword()+";"+u.getIsUser());
			setForwardView(MAIN_JSP);
		}else if(u.getIsUser() == 1) {
			//学生
			System.out.println("登录人员=="+u.getUsername()+";"+u.getPassword()+";"+u.getIsUser());
			setForwardView(STUDENT_MAIN_JSP);
		}
		return SUCCESS;
	}
	
	public String list() {
		list = userService.findAllList(user);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	public String edit() {
		userService.edit(user);
		return list();
	}
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getForwardView() {
		return forwardView;
	}
	public void setForwardView(String forwardView) {
		this.forwardView = forwardView;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}
	
	
}
